﻿#include "testAddMenuItem.hpp"
testMenu::testMenu(QString name, QString icon, QString key)
    : m_name(name), m_icon(icon), m_shortcut_key(key)
{
    //QLabel* labb = new QLabel(this);
    //m_layout = new QHBoxLayout();
    //m_layout->addWidget(labb);
    //m_layout->setMargin(40);
    //m_layout->setSpacing(0);
    //
    //setLayout(m_layout);
    //labb->setStyleSheet("background-color:red;");

    m_layout = new QHBoxLayout();
    iconshow = new QLabel;//装图标的QLabel
    auto* pixmap = new QPixmap(m_icon);
    pixmap->scaled(16, 16, Qt::KeepAspectRatio);
    iconshow->setPixmap(*pixmap);//显示icon
    auto pixsize = pixmap->size();//获取图标的大小
    //iconshow->setMaximumSize();
    nameshow = new QLabel;//装菜单项字体的QLabel
    nameshow->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    nameshow->setText(m_name);
    nameshow->setAlignment(Qt::AlignLeft);//设置左对齐
    //QFont font("Times New Roman", 10);//14字体大小
    //nameshow->setFont(font);
    if (m_shortcut_key.isEmpty())
    {
        //占位符不启作用，强制使用空格占位
        m_shortcut_key = "                            ";
    }


    if (!m_shortcut_key.isEmpty())
    {
        //如果快捷键显示字符串不为空
        shortcut_keyshow = new QLabel;
        shortcut_keyshow->setAlignment(Qt::AlignRight);//设置右对齐
        QPalette palette;
        palette.setColor(QPalette::WindowText, QColor(122, 122, 144));//快捷键显示的颜色
        shortcut_keyshow->setPalette(palette);
        shortcut_keyshow->setText(m_shortcut_key);
    }
    m_layout->addWidget(iconshow);//添加图标显示到布局
    m_layout->setSpacing(0);//设置图标和字符串的间隔

    m_layout->addWidget(nameshow);//添加菜单名显示到布局

    if (!m_shortcut_key.isEmpty())
    {
        //如果快捷键名称不为空
        m_layout->setSpacing(0);//设置字符串和快捷显示的间隔
        m_layout->addWidget(shortcut_keyshow);//添加快捷键显示名到布局
    }
    setLayout(m_layout);//为widget设置布局
    m_layout->setMargin(0);
    setFixedSize(200, 30);
}

void testMenu::onClick()
{
    QMessageBox* me = new QMessageBox();
    me->setText("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    me->exec();
	qDebug() << u8"testMenu 被点击了";
}
